import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter_base/utils/log/utils_log.dart';

import '../obx/base_obx.dart';

class BaseAnimatedBuilder extends AnimatedBuilder {
  final ListenerChange? listenerChange;

  BaseAnimatedBuilder({super.key, required super.animation, required super.builder, super.child, this.listenerChange});

  Widget? _content;
  int? _listHashCode;

  @override
  Widget build(BuildContext context) {
    if (listenerChange == null || (listenerChange!.call() ?? []).isEmpty) {
      clog("===> ${runtimeType.toString()} animate build child", tag: 'animation');
      return super.build(context);
    } else {
      int? code = jsonEncode(listenerChange!.call()).hashCode;
      if (code != _listHashCode) {
        clog("===> ${runtimeType.toString()} animate build child", tag: "animation");
        _content = super.build(context);
        _listHashCode = code;
      }
      return _content ?? const SizedBox();
    }
  }
}
